# # -*- coding:utf-8 -*-
# class Solution:
#     # 返回[a,b] 其中ab是出现一次的两个数字
#     def FindNumsAppearOnce(self, array):
#         # write code here
#         res = dict()
#         for num in array:
#             if num in res.keys():
#                 res[num] += 1
#             else:
#                 res[num] = 1
#         return_num = []
#         for key, value in res.items():
#             if value == 1:
#                 return_num.append(key)
#         return return_num

class Solution:
    # 返回[a,b] 其中ab是出现一次的两个数字
    def FindNumsAppearOnce(self, array):
        '''
        异或之后相当于两个不同的数异或，那么第一个不为0的数一定来自于这两个数之一
        因此将所有这个位置上不为1的数异或之后就得到不一样的数
        :param array:
        :return:
        '''
        if not array:
            return []
        tmp = 0
        for num in array:
            tmp ^= num
        # print(tmp)
        idx = 0
        while tmp | 1 != tmp:
            idx += 1
            tmp = tmp >> 1
        # print(idx)
        a = 0
        b = 0
        for num in array:
            if self.helper(num, idx):
                a  = a^num
            else:
                b = b^num
        return [a, b]

    def helper(self, num, idx):
        return (num >> idx) & 1

test = Solution()
print(test.FindNumsAppearOnce([2,4,3,6,3,2,5,5]))